var add_banner;
var edit_banner;
var add_template_template;


function createCustDropzone(json){
    var that = this;
    this.url = json.url || "empty.php";
    this.selector = json.selector;
    this.preview = json.preview;
    this.previewTemplate = json.template;
    this.clickable = json.clickable;
    this.dropzone = null;
    this.postparam = {};

    this.getPostParam = function(){
        return that.postparam;
    };
    this.destroyDropzone = function(){
        if(this && typeof this !== undefined){
            this.dropzone.destroy();
        }
    };
    this.loadDzTemp = function(json){
        var imgFile = json.data;
        var imgInfo = "";
        var imgName = "";
        var imgPath = "";
        var mockFile = {};
        if(!$.isEmptyObject(imgFile)){
            imgInfo = (imgFile.name).split("/");
            imgName = imgInfo.pop();
            imgPath = imgInfo.join("/");
            mockFile = { name: imgName, accepted:true ,type:imgFile.type,size:imgFile.size,id:imgFile.id,path:imgPath};
            this.dropzone.emit("addedfile",mockFile);
            this.dropzone.emit("thumbnail", mockFile, imgFile.name);
            this.dropzone.emit("complete", mockFile);

        }else{
            return false;
        }
    };
    this.uploadedStyle = function(file){
        //styles
        var deleteBtn =  file.previewElement.querySelector(".delete");
        // var downloadBtn = file.previewElement.querySelector(".btn-download");
        file.previewElement.querySelector(".start").remove();
        file.previewElement.querySelector(".cancel").remove();
        deleteBtn.style.display = "inline-block";
        // downloadBtn.style.display = "inline-block";
        file.previewElement.querySelector(".progress").style.opacity = 0;
        deleteBtn.setAttribute("title","");
        // downloadBtn.setAttribute("href","");
    };

    this.dropzone = new Dropzone(this.selector,{
        url: that.url,
        thumbnailWidth: 80,
        thumbnailHeight: 80,
        parallelUploads: 1,
        previewTemplate: this.previewTemplate,
        autoQueue: false,
        maxFiles: 1,
        previewsContainer: this.preview,
        clickable: this.clickable
    });
    this.dropzone.on("addedfile", function(file) {

        if(file.id && file.id!=''){
            that.postparam.id = file.id;
            that.postparam.type = file.type;
            that.postparam.size = file.size;
            that.postparam.name = file.path + "/" + file.name;
            dzUploadedStyle(file);
            file.previewElement.id = file.id;
        }else{
            that.postparam = {};
            that.postparam.id = ((new Date()).getTime()).toString();
            file.id = that.postparam.id;
            if (this.files.length > 1) {
                this.removeFile(this.files[0]);
            }
            file.previewElement.querySelector(".start").onclick = function() {
                that.dropzone.enqueueFile(file);
            };
        }
    });

    this.dropzone.on("sending", function(file) {
        file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
    });

    this.dropzone.on("success", function(file,response) {
        var params = {};
        var $download = $(file.previewElement).find(".name").children("a");
        params.name = file.name;
        params.type = file.type;
        params.size = file.size;
        params.id = file.id;
        file.previewElement.id = params.id;

        $download.attr({href:params.name});

        that.postparam = params;

        that.uploadedStyle(file);
    });

    this.dropzone.on("removedfile", function(file) {                                              //TODO:删除图片
        var deleteId = file.previewElement.id;
    });
}

function getDzTemplate(templateseletor){
    var previewNode,previewTemplate;
    previewNode = document.querySelector(templateseletor);
    previewNode.id = "";
    previewTemplate = previewNode.parentNode.innerHTML;
    previewNode.parentNode.removeChild(previewNode);
    return previewTemplate;
}

function initDzFn(){
    add_template_template = getDzTemplate("#template-1");
}

function closePanel(){

}
initDzFn();